<template>
  <div>
    <!-- Breadcrumb Navigation area -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">Home</el-breadcrumb-item>
      <el-breadcrumb-item>Order</el-breadcrumb-item>
      <el-breadcrumb-item>Orders</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- card view area -->
    <el-card>
      <!-- search & add btn area -->
      <el-row :gutter="13">
        <el-col :span="7">
          <el-input placeholder="Please input key">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- order list -->
      <el-table :data="orderList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="Order Nr" prop="order_number"></el-table-column>
        <el-table-column label="Order Price" prop="order_price"></el-table-column>
        <el-table-column label="Is Pay">
          <template slot-scope="sp">
            <el-tag type="success" v-if="sp.row.pay_status === '1'">Paid</el-tag>
            <el-tag type="danger" v-if="sp.row.pay_status === '0'">Unpaid</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="Is Send" prop="is_send"></el-table-column>
        <el-table-column label="Order Time">
          <template slot-scope="sp">{{sp.row.create_time | dateFormat}}</template>
        </el-table-column>
        <el-table-column label="Opearting">
          <template>
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="showEditAddressDialog"
              size="mini"
            ></el-button>
            <el-button
              type="success"
              icon="el-icon-location"
              size="mini"
              @click="showLogisticsProgressBox"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- paging -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 3, 5, 10, 50, 100]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      ></el-pagination>
    </el-card>

    <!-- Edit adress dialog  -->
    <el-dialog
      title="Change address"
      @close="closeEditAddressDialog"
      :visible.sync="editAddressDialogVisible"
      width="57%"
    >
      <el-form
        :model="editAddressForm"
        :rules="editAddressFormRules"
        ref="editAddressFormRef"
        label-width="100px"
      >
        <el-form-item label="PCD/County" prop="address1">
          <el-cascader :options="cityData" v-model="editAddressForm.address1"></el-cascader>
        </el-form-item>
        <el-form-item label="Address" prop="address2">
          <el-input v-model="editAddressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editAddressDialogVisible = false">Cancel</el-button>
        <el-button type="primary" @click="editAddressDialogVisible = false">I Sure</el-button>
      </span>
    </el-dialog>

    <!-- Logistics progress diglog -->
    <el-dialog
      title="Logistics Progress"
      :visible.sync="LogisticsProgressDialogVisible"
      width="57%"
    >
      <!-- timeline for logistics progress -->
      <el-timeline>
        <el-timeline-item
          v-for="(activity, idx) in logisticsProgressInfo"
          :key="idx"
          :timestamp="activity.activity"
        >{{activity.context}}</el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityData from './citydata.js'

export default {
  created() {
    this.getOrderList()
  },
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      orderList: [],
      editAddressDialogVisible: false,
      editAddressForm: {
        address1: [],
        address2: ''
      },
      editAddressFormRules: {
        address1: [
          {
            required: true,
            message: 'Please choose Province city district/County',
            trigger: 'blur'
          }
        ],
        address2: [
          {
            required: true,
            message: 'Please input address',
            trigger: 'blur'
          }
        ]
      },
      cityData,
      LogisticsProgressDialogVisible: false,
      waybillNumber: '804909574412544580',
      logisticsProgressInfo: []
    }
  },
  methods: {
    async getOrderList() {
      const rs = await this.$http.get('orders', {
        params: this.queryInfo
      })
      if (rs.meta.status !== 200) {
        return this.$msg.error('get order list faild!')
      }
      this.total = rs.data.total
      this.orderList = rs.data.goods
      // console.log(this.orderList)
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },
    showEditAddressDialog() {
      this.editAddressDialogVisible = true
    },
    closeEditAddressDialog() {
      this.$refs.editAddressFormRef.resetFields()
    },
    async showLogisticsProgressBox() {
      /* get logistics progress from server */
      const rs = await this.$http.get(`kuaidi/${this.waybillNumber}`)
      if (rs.meta.status !== 200) {
        return this.$msg.error('Get logistics progress faild!')
      }
      this.logisticsProgressInfo = rs.data
      console.log(this.logisticsProgressInfo)
      this.LogisticsProgressDialogVisible = true
    }
  }
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
